package com.leetcode;

/**
 * 744. 寻找比目标字母大的最小字母
 * 二分搜索法O(logn)
 *
 * @author fy
 * @date 2022/4/27 14:38
 */
public class Solution744 {

    public char nextGreatestLetter(char[] letters, char target) {
        int n = letters.length;
        int left = 0;
        int right = n - 1;
        while (left <= right) {
            int mid = left + ((right - left) >> 1);
            char ch = letters[mid];
            if (ch > target) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        // 二分查找, 同时优雅处理超出范围
        return letters[left % n];
    }

    public static void main(String[] args) {
        char[] letters = new char[]{'e', 'e', 'e', 'e', 'e', 'e', 'n', 'n', 'n', 'n' };
        char target = 'e';
        char res = new Solution744().nextGreatestLetter(letters, target);
        System.out.println(res);
    }

}
